<script>
import useStore from '../../store/index.js';
const {user} = useStore()
  export default {
    name:"my-address",
    data() {
      return {
        address:{}
      };
    },
    methods:{
      async chooseAddress() {
       const res = await uni.chooseAddress().catch(err => err)
       if (res.errMsg === 'chooseAddress:ok') {
         user.upDateAdress(res)
       }
      },
    },
    computed:{
      addressInfo(){
        return user.address
      },
      addStr(){
        return user.addressStr
      }
    }
  }
</script>

<!-- {"errMsg":"chooseAddress:ok","userName":"张三","nationalCode":"510000","telNumber":"020-81167888","postalCode":"510000","provinceName":"广东省","cityName":"广州市","countyName":"海珠区","detailInfo":"新港中路397号"} -->

<template>
  <view>
    <!-- 选择收货地址的盒子 -->
    <view class="address-choose-box" v-if="JSON.stringify(this.addressInfo) === '{}'">
      <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
    </view>
      <!-- 渲染收货信息的盒子 -->
    <view class="address-info-box" @click="chooseAddress()" v-else>
      <view class="row1">
        <view class="row1-left">
          <view class="username">收货人：<text>{{this.addressInfo.userName}}</text></view>
        </view>
        <view class="row1-right">
          <view class="phone">电话：<text>{{this.addressInfo.telNumber}}</text></view>
          <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <view class="row2-left">收货地址：{{this.addStr}}</view>
        <view class="row2-right"></view>
      </view>
    </view>
    <!-- 底部图片 -->
    <image src="/static/cart_border@2x.png" class="address-bottom"></image>
  </view>
</template>

<style lang="scss">
.address-choose-box{
  display: flex;
  height: 90px;
  justify-content: center;
  align-items: center;
}

.address-bottom{
  height: 5px;
  width: 100%;
  display: block;
}


// 渲染收货信息的盒子
.address-info-box {
  font-size: 12px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 5px;

  // 第一行
  .row1 {
    display: flex;
    justify-content: space-between;

    .row1-right {
      display: flex;
      align-items: center;

      .phone {
        margin-right: 5px;
      }
    }
  }

  // 第二行
  .row2 {
    display: flex;
    align-items: center;
    margin-top: 10px;

    .row2-left {
      white-space: nowrap;
    }
  }
}
</style>